<?php
/**
 * Created by PhpStorm.
 * @User: 王崇全
 * @Date: 2019/1/24 11:54
 */

namespace app\api\controller;


use app\common\controller\BaseController;
use java\JavaBridge;

class TdxController extends BaseController
{

    //登录验证端口
    private const API_URL = "http://hlc.ronmei.net/api/user/checkUser";

    /**
     * 登录
     * @author 王崇全
     * @date   2019/1/24 11:56
     * @return void
     * @throws
     */
    public function login()
    {

        $in = $this->I([
            [
                "user_name|用户名",
                null,
                "s",
                "require",
            ],
            [
                "pwd|密码",
                null,
                "s",
                "require",
            ],
        ]);

        //验证用户名和密码是否匹配
        $res = send_http(self::API_URL, [
            "username" => $in['user_name'],
            "password" => $in['pwd'],
        ]);
        $res = json_decode($res, true);

        if (!isset($res['code']) || $res['code'] != 0) {
            rj(1, "用户名或密码错误");
        }

        $jb = new JavaBridge();

        $jb->login();

        rj();
    }

    /**
     * 发送订单
     * @author 王崇全
     * @date   2019/1/24 12:09
     * @return void
     * @throws
     */
    public function sendOrder()
    {
        $in = $this->I([
            [
                "category|委托种类",
                null,
                "d",
                "require",
            ],
            [
                "price_type|报价方式",
                null,
                "d",
                "require",
            ],
            [
                "code|股票代码",
                null,
                "d",
                "require",
            ],
            [
                "price|价格",
                null,
                "f",
                "require",
            ],
            [
                "quantity|数量",
                null,
                "d",
                "require",
            ],
        ]);
        $jb = new JavaBridge();
        $res = $jb->SendOrder($in['category'], $in['price_type'], $in['code'], $in['price'], $in['quantity']);

        rjd($res);
    }

    /**
     * 撤销订单
     * @author 王崇全
     * @date   2019/1/24 12:09
     * @return void
     * @throws
     */
    public function cancelOrder()
    {
        $in = $this->I([
            [
                "code|股票代码",
                null,
                "s",
                "require",
            ],
            [
                "hth|委托编号",
                null,
                "s",
                "require",
            ],
        ]);

        $jb = new JavaBridge();

        $res = $jb->cancelOrder($in['code'], $in['hth']);

        rjd($res);
    }

    /**
     * 查询历史数据
     * @author 王崇全
     * @date   2019/1/24 12:10
     * @return void
     * @throws \Exception
     */
    public function queryHistoryData()
    {
        $in = $this->I([
            [
                "category|种类",
                null,
                "d",
                "require",
            ],
            [
                "startDate|开始日期",
                null,
                "s",
                "require",
            ],
            [
                "endDate|结束日期",
                null,
                "s",
                "require",
            ],
        ]);

        $jb = new JavaBridge();

        $res = $jb->QueryHistoryData($in['category'], $in['startDate'], $in['endDate']);

        rjd($res);
    }

    /**
     * 查询各种交易数据
     * @author 王崇全
     * @date   2019/1/24 12:10
     * @return void
     * @throws \Exception
     */
    public function queryData()
    {
        $in = $this->I([
            [
                "category|种类",
                null,
                "d",
                "require",
            ],
        ]);

        $jb = new JavaBridge();

        $res = $jb->queryData($in['category']);

        rjd($res);
    }

}